"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--CPU
Case Name   : COPY时主节点CPU占用率100%
Create At   : 2021/07/12
@zou_jialiang0505328126
Description :
    1.创建表并插入数据
    2.主节点注入CPU占用率100%故障
    3.将t1表内容拷贝到t.sql文件
    4.将t.sql文件内容拷贝到表t2
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
    Modified by wx1115623 2023/2/16:修改注入CPU占用率故障断言信息
"""

import unittest
import os
import time
from yat.test import Node, macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger


class CPUOverloadAndRunTPCC(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_root_node = Node(node='PrimaryRoot')
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.com = Common()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.tb_name1 = "cpu_tb1"
        self.tb_name2 = "cpu_tb2"
        self.file_name = os.path.join(macro.DB_INSTANCE_PATH, "copy_data.sql")
        self.constant = Constant()

    def test_main(self):
        self.log.info("----------------1.创建表-------------------")
        sql = f"create table if not exists " \
            f"{self.tb_name1}(i int, c varchar(10));" \
            f"create table if not exists " \
            f"{self.tb_name2}(i int, c varchar(10));" \
            f"insert into {self.tb_name1} " \
            f"values(generate_series(1,10000), 'test');"
        result = self.primary_sh.executDbSql(sql)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result)

        self.log.info("--------------2.主节点注入CPU占用率高故障-------------")
        shell_cmd = "cat /proc/cpuinfo |grep 'processor'|wc -l"
        self.log.info(shell_cmd)
        cpu_num = self.primary_root_node.sh(shell_cmd).result()
        self.log.info(cpu_num)
        inject_command = f"rCPU_Overloadau (cpuid1, cpuid2, usage, time) " \
            f"values(0, {int(cpu_num)-1}, 100, 20)"
        result = self.com.cfe_inject(self.primary_root_node, inject_command)
        self.log.info(result)
        flag = self.constant.cfe_inject_rcpu_overloadau_success_msg in result
        if not flag:
            res = self.com.cfe_inject(self.primary_root_node, inject_command)
            self.log.info(res)
            self.assertIn(self.constant.cfe_inject_rcpu_overloadau_success_msg,
                          res.strip())

        self.log.info("-------------3.将t1表内容拷贝到t.sql文件--------------")
        sql = f"COPY (select * from {self.tb_name1}) to '{self.file_name}';"
        result = self.primary_sh.executDbSql(sql)
        self.log.info(result)
        self.assertIn('COPY 10000', result)

        self.log.info("-------------4.将t.sql文件内容拷贝到表t2--------------")
        sql = f"COPY {self.tb_name2} from '{self.file_name}';"
        result = self.primary_sh.executDbSql(sql)
        self.log.info(result)
        self.assertIn('COPY 10000', result)

        self.log.info("-------------比较两表数据-------------------")
        sql = f"COPY (select * from {self.tb_name2}) to '{self.file_name}1';"
        result = self.primary_sh.executDbSql(sql)
        self.log.info(result)
        self.assertIn('COPY 10000', result)

        cmd = f"diff {self.file_name}1 {self.file_name}"
        self.log.info(cmd)
        result = self.primary_user_node.sh(cmd).result()
        self.log.info(result)
        self.assertEqual("", result.strip())

    def tearDown(self):
        self.log.info("------------------删除表-----------------")
        time.sleep(20)
        sql = f"drop table if exists {self.tb_name1} cascade;" \
            f"drop table if exists {self.tb_name2} cascade;"
        result = self.primary_sh.executDbSql(sql)
        self.log.info(result)
        self.log.info("-------------删除备份文件-------------------")
        cmd = f"rm -rf {self.file_name};rm -rf {self.file_name}1"
        result = self.primary_user_node.sh(cmd).result()
        self.log.info(result)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
